package com.bocai.kelalacar.ui.car.main.ada;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelalacar.R;
import com.bocai.kelalacar.bean.CarDriverBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yuanfei on 2017/6/29.
 */

public class SpinnerDriverAdapter extends BaseAdapter {

    private List<CarDriverBean.ContentBean.DriverListBean> mList;
    private Context mContext;

    public SpinnerDriverAdapter(Context pContext, List<CarDriverBean.ContentBean.DriverListBean> pList) {
        this.mContext = pContext;
        this.mList = pList;
    }


    @Override
    public int getCount() {
        return mList != null ? mList.size()+2 : 2;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_spinner_address, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (position == 0) {
            viewHolder.ll1.setVisibility(View.VISIBLE);
            viewHolder.ll2.setVisibility(View.GONE);
            viewHolder.ll3.setVisibility(View.GONE);
            viewHolder.tvSelect.setText("请选择司机");
        } else if (position == mList.size() + 1) {
            viewHolder.ll1.setVisibility(View.GONE);
            viewHolder.ll2.setVisibility(View.GONE);
            viewHolder.ll3.setVisibility(View.VISIBLE);
            viewHolder.tvAdd.setText("添加司机");
        } else {
            viewHolder.ll1.setVisibility(View.GONE);
            viewHolder.ll2.setVisibility(View.VISIBLE);
            viewHolder.ll3.setVisibility(View.GONE);
            viewHolder.tv1.setText(mList.get(position -1).getDriverName());
            viewHolder.tv2.setText(mList.get(position -1).getDriverPhone());
        }

        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_select)
        TextView tvSelect;
        @BindView(R.id.ll1)
        LinearLayout ll1;
        @BindView(R.id.tv1)
        TextView tv1;
        @BindView(R.id.tv2)
        TextView tv2;
        @BindView(R.id.ll2)
        LinearLayout ll2;
        @BindView(R.id.tv_add)
        TextView tvAdd;
        @BindView(R.id.ll3)
        LinearLayout ll3;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public void refreshData(List<CarDriverBean.ContentBean.DriverListBean> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

}
